﻿/*
 * Created by SharpDevelop.
 * User: Edward Wilde (http://blogs.charteris.com/edwardw)
 * Date: 05/07/2007
 * Time: 17:21
 */

using Wax;
using NUnit.Framework;
using WatiN.Core;
using System;

namespace WaxLibTest
{
	/// <summary>
	/// Tests the behaviour of the <see cref="SetValueCommand"/>.
	/// </summary>
	[TestFixture]
	public class SetValueCommandTest
	{
		#region Public instance methods
		
		/// <summary>
		/// Tests the execution of the text command
		/// </summary>
		[Test]
		public void SetTextFieldValue()
		{
			using (TestManager testManager = new TestManager())
			{
				testManager.Tests.Add(new Test());
				testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
				testManager.Tests[0].Commands.Add(CreateCommandText("Edward,Text", "ById", "FirstName"));
				
				testManager.Execute();
				
				Assert.AreEqual("Edward", testManager.Browser.TextField("FirstName").Value, "Command.Execute using a text command did not function appropriately");
			}
		}
		
		/// <summary>
		/// Test that we can set the value of a file input element
		/// </summary>
		[Test]
		public void SetFileUploadValue()
		{
			using (TestManager testManager = new TestManager())
			{
				string testHtmlPage = System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm");
				testManager.Tests.Add(new Test());
				testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(testHtmlPage));
				testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] {"SetFileUpLoad", "SetValue", string.Format("{0},FileName", testHtmlPage), "ById", "fileUpload"}));
				
				testManager.Execute();
				
				Assert.AreEqual(testHtmlPage, testManager.Browser.FileUpload("fileUpload").FileName, "SetValueCommand.Execute using a filename command argument did not function appropriately");
			}
		}
		
		/// <summary>
		/// Test that we can set the value of a radio button group
		/// </summary>
		[Test]
		public void SetRadioButtonValue()
		{
			using (TestManager testManager = new TestManager())
			{
				testManager.Tests.Add(new Test());
				testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
				testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] {"SetRadBtn1", "SetValue", "true,Checked", "ByNameAndValue", "radio1,2"}));
				
				testManager.Execute();
				
				Assert.AreEqual(true, testManager.Browser.RadioButton(Find.ByName("radio1") && Find.ByValue("2")).Checked, "SetValueCommand.Execute using a checked command argument did not function appropriately");
			}
		}
		
		/// <summary>
		/// Test that we can set the value of a checkbox
		/// </summary>
		[Test]
		public void SetCheckBoxValue()
		{
			using (TestManager testManager = new TestManager())
			{
				testManager.Tests.Add(new Test());
				testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
				testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] {"SetChk", "SetValue", "false,Checked", "ById", "chkEmailMe"}));
				
				testManager.Execute();
				
				Assert.AreEqual(false, testManager.Browser.CheckBox(Find.ById("chkEmailMe")).Checked, "SetValueCommand.Execute using a checked command argument did not function appropriately");
			}
		}
		
		
		/// <summary>
		/// Test that we can set the value of a select list
		/// </summary>
		[Test]
		public void SetSelectListValue()
		{
			using (TestManager testManager = new TestManager())
			{
				testManager.Tests.Add(new Test());
				testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
				testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] {"SetCardList", "SetValue", "2,SelectedItem", "ById", "cardList"}));
				
				testManager.Execute();
				
				Assert.AreEqual("2", testManager.Browser.SelectList(Find.ById("cardList")).SelectedOption.Value, "SetValueCommand.Execute using a select list command argument did not function appropriately");
			}
		}
		
		/// <summary>
		/// Test that a <see cref="SetValueCommand" /> of type <see cref="CommandType.SetValues" /> correctly parses the <see cref="SetValueCommand.ValuesToSet"/>
		/// </summary>
		[Test]
		public void ValuesToSet()
		{
			SetValueCommand multipleSetValues = (SetValueCommand) CommandFactory.Parse(new String[] {"SetCardList", "SetValues", "1,2,3,SelectedItem", "ById", "cardList2"});
		
			Assert.IsNotNull(multipleSetValues.ValuesToSet, "Incorrectly parsed ValuesToSet property");
			Assert.AreEqual(3, multipleSetValues.ValuesToSet.Count, "Incorrectly parsed ValuesToSet property");
		}
		
		/// <summary>
		/// Test that we can set multiple values of a select list
		/// </summary>
		[Test]
		public void SetSelectListMultipleValues()
		{
			using (TestManager testManager = new TestManager())
			{
				testManager.Tests.Add(new Test());
				testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
				testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] {"SetCardList", "SetValues", "1,2,3,SelectedItem", "ById", "cardList2"}));
				
				testManager.Execute();
				
				SelectList cardList = testManager.Browser.SelectList(Find.ById("cardList2"));
				Assert.AreEqual(3, cardList.SelectedOptions.Count, "SetValueCommand.Execute using a select list command argument did not function appropriately");
			}
		}
		#endregion
		
		#region Private static methods
		
		/// <summary>
		/// Creates a command that sets the value of a <see cref="WaitN.Core.TextField">TextField</see>
		/// </summary>
		/// <param name="commandArgs">Command arguments</param>
		/// <param name="findByMethod">Find by method name</param>
		/// <param name="findByMethodArgs">Arguments to pass to the find method</param>
		/// <returns></returns>
		internal static Command CreateCommandText(string commandArgs, string findByMethod, string findByMethodArgs)
		{
			return CommandFactory.Parse(new string[] {"textField", "SetValue", commandArgs, findByMethod, findByMethodArgs});
		}
		
		#endregion
	}
}
